package simtester.server

import simtester.server.TestCase;
import simtester.server.User;

class TestCaseController {

    def temp() {
        def u = new User(username: 'Linh', password: 'pass')
        u.save()

        def tc = new TestCase(name: 'mytest', owner: u)
        
        if (tc.save()) {
            flash.message = 'Saved'
        } else {
            flash.error = 'Not saved'
        }
        render (view: 'temp', model: [testcase: tc])
    }
    
    def create() {
        if (request.method == 'POST') {
            def u = User.findByUsername(session.user)
            def tc = new TestCase(name: params.name,
                                  script: request.getFile('script').inputStream.text,
                                  owner: u,
								  type: params.addtypeselectlist)

            def team = u.team
            team.addToTestcases(tc)
            team.save()
			
            redirect(controller: 'testRun', action: 'create')
        }
    }

    def update() {
		def tc = TestCase.get(params.tcid.toLong())
		tc.name = params.name
		tc.type = params.type
		
		def tmpParams = params.findAll { key, value ->
			key != 'name' &&
			key != 'tcid' &&
			key != 'controller' &&
			key != 'action'
		}

		tmpParams.each { key, value ->
			tc.setCategory(key.toLowerCase(), value)
		}
		
		tc.save(failOnError: true)
		
		redirect(controller: 'testRun', action: 'create')
	}
	
	def delete() {
		def tc = TestCase.get(params.tcid.toLong())
		tc.delete()
		redirect(controller: 'testRun', action: 'create')
	}
    
}
